<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="产品名称" prop="productName">
        <el-input v-model="dataForm.productName" placeholder="产品名称"></el-input>
      </el-form-item>
      <el-form-item label="周期(月)" prop="cycle" >
        <el-input v-model="dataForm.cycle" placeholder="周期"></el-input>
      </el-form-item>
      <el-form-item label="开始时间" prop="beginTime" >
        <el-date-picker
          v-model="dataForm.beginTime"
          type="datetime"
          format="yyyy-MM-dd HH:mm:ss"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择日期时间"/>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime" >
        <el-date-picker
          v-model="dataForm.endTime"
          type="datetime"
          format="yyyy-MM-dd HH:mm:ss"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择日期时间"/>

      </el-form-item>
      <el-form-item label="价格" prop="price" >
        <el-input v-model="dataForm.price" placeholder="价格"></el-input>
      </el-form-item>
      <el-form-item label="产品描述" prop="description">
        <el-input v-model="dataForm.description" type="textarea" placeholder="产品描述"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { isEmail, isMobile } from '@/utils/validate'
  export default {
    data () {


      return {
        visible: false,
        roleList: [],
        pickerOptions: {
          disabledDate(time) {
            return time.getTime() > Date.now();
          },
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }]
        },
        dataForm: {
          id: 0,
          productName: '',
          cycle: 3,
          beginTime: '',
          endTime: '',
          price: '',
          description: '',
          remark: ''
        },
        dataRule: {
          productName: [
            { required: true, message: '产品名称不能为空', trigger: 'blur' }
          ],
          cycle: [
            { required: true, message:'周期不能为空' , trigger: 'blur' }
          ],
          beginTime: [
            { required: true, message:'开始时间不能为空' , trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '结束时间不能为空', trigger: 'blur' },
          ],
          price: [
            { required: true, message: '价格不能为空', trigger: 'blur' },
          ],
          description: [
            {required: true, message: '描述不能为空', trigger: 'blur'}
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' },
          ],
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.$http({
          url: this.$http.adornUrl('/sys/role/select'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          this.roleList = data && data.code === 0 ? data.list : []
        }).then(() => {
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
          })
        }).then(() => {
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/cProduct/info`),
              method: 'post',
              params: this.$http.adornParams({
                id:this.dataForm.id
              })
            }).then(({data}) => {
              if (data && data.code === 0) {debugger
                var t=data.data;
                this.dataForm.id = t.id
                this.dataForm.productName = t.productName
                this.dataForm.description = t.description
                this.dataForm.price = t.price
                this.dataForm.cycle = t.cycle
                this.dataForm.beginTime = t.beginTime
                this.dataForm.endTime = t.endTime
                this.dataForm.remark=t.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/cProduct/saveOrUpdate`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'productName': this.dataForm.productName,
                'description': this.dataForm.description,
                'price': this.dataForm.price,
                'cycle': this.dataForm.cycle,
                'beginTime': this.dataForm.beginTime,
                'endTime': this.dataForm.endTime,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
